package com.example.myapplication.ui.download

import androidx.room.*
import com.example.myapplication.ui.FileBean
import com.example.myapplication.ui.upload.FileUploadBean
import kotlinx.coroutines.flow.Flow

@Dao
interface FileDownloadDao {
    /**
     * 获取所有任务
     */
    @Query("SELECT * FROM file_download_table ORDER BY name ASC")
    fun getAlphabetizedWords(): List<FileDownloadBean>
    /**
     * 获取指定任务
     */
    @Query("SELECT * FROM file_download_table WHERE uuid=:uuid limit 1")
    fun getFileDownloadBeanByUUID(uuid: String): FileDownloadBean?
    /**
     * 获取单个准备任务
     */
    @Query("SELECT * FROM file_download_table WHERE enabled = 1 and downloadState = 3 ORDER BY createAt ASC limit 1")
    fun getOtherNotDownloadTask(): FileDownloadBean?
    /**
     * 获取任务列表
     */
    @Query("SELECT * FROM file_download_table ORDER BY  downloadState,type ASC")
    fun getAlphabetizedWordsByFlow(): Flow<List<FileDownloadBean>>
    /**
     * 插入任务
     */
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insert(word: FileDownloadBean)
    /**
     * 更新所有未完成任务
     */
    @Query("UPDATE OR ABORT file_download_table SET   updateAt =:updateAt , state =:state , downloadState = 2  WHERE `length` <> `downloadSize` and enabled = 1 and  downloadState <> 3 ")
    fun updateAllTask(
        state: String = "${FileBean.STATE_NET_FLAG}网络异常，请点击重新下载",
        updateAt: Long = System.currentTimeMillis()
    )
    /**
     * 更新指定UUID任务
     */
    @Query("UPDATE OR ABORT file_download_table SET length =:length , downloadSize =:downloadSize , downloadState = :downloadState, updateAt =:updateAt , state =:state , downloadSpeed =:downloadSpeed WHERE `uuid` = :uuid ")
    fun update(
        uuid: String,
        length: Long,
        downloadSize: Long,
        downloadState: Byte,
        downloadSpeed: Long,
        state: String,
        updateAt: Long = System.currentTimeMillis()
    )
    /**
     * 更新所有的异常任务
     */
    @Query("UPDATE OR ABORT file_download_table SET downloadState = 3 , updateAt =:updateAt , state = 'DEFAULT_正在等待...'  WHERE downloadState = 2 ")
    fun updateErrorAll(updateAt: Long = System.currentTimeMillis()): Int
    /**
     * 移除指定任务
     */
    @Query("DELETE FROM file_download_table Where `uuid` = :id")
    fun deleteByUUID(id: String): Int
    /**
     * 移除所有完成任务
     */
    @Query("DELETE FROM file_download_table where enabled = 1 and (`downloadState` = 4 or `downloadSize` = `length`)")
    fun deleteFinishAll(): Int
    /**
     * 移除所有运行任务
     */
    @Query("DELETE FROM file_download_table where enabled = 1 and `downloadState` = 1")
    fun deleteRunningAll(): Int
    /**
     * 删除所有异常数据
     */
    @Query("DELETE FROM file_download_table where enabled = 1 and (`downloadState` = 2 or `downloadSize` <> `length`)")
    fun deleteErrorAll(): Int
    /**
     * 删除所有
     */
    @Query("DELETE FROM file_download_table")
    fun deleteAll(): Int
    /**
     * 更新所有数据
     */
    @Update(entity = FileDownloadBean::class)
    fun updateAll(vararg card: FileDownloadCard)

}